package cn.com.shopec.erp.base.controller;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.com.shopec.erp.base.dao.IPurchaserOrder;
import cn.com.shopec.erp.base.service.IPurchaseOrderTypeService;
import cn.com.shopec.erp.common.constant.SZSCMConstant;
import cn.com.shopec.erp.common.page.PageFinder;
import cn.com.shopec.erp.common.page.PageQuery;
import cn.com.shopec.erp.common.utils.JsonHelper2;
import cn.com.shopec.erp.financing.entity.PurchaserOrder;
import cn.com.shopec.erp.financing.entity.PurchaserOrderVo;
import cn.com.shopec.erp.purchase.commo.POPropertyUtils;
import cn.com.shopec.erp.system.service.IRoleService;
import common.Logger;

/**
 * @ClassName: PurchaseOfSettlementController
 * @Description: 财务管理-购销单Controller
 * @author: gujianfeng
 * @date 2013-5-25 下午2:10:32
 */
@RequestMapping("/suppliers/suppcontral")
@Controller
public class PurchaseOfSettlementController {

    @Resource
    private IPurchaseOrderTypeService pot;
    private Logger log = Logger.getLogger(PurchaseOfSettlementController.class);
    @Resource
    private IRoleService roleService;
    @Resource
    private IPurchaserOrder iPurchaserOrder;
    @Resource
    private IPurchaseOrderTypeService purchaseOrderTypeService;


    /**
     * @methodName：addAuditSetting
     * @Description: 跳转单据有效设置
     * @return
     * @author: gaoyaxiong
     * @date: 2013-5-22 下午15:10:03
     */
    @RequestMapping("purcherType")
    public String addPurcherType() {
        return "/base/receipts/set_receipts";
    }

    /**
     * 采购单类型设置
     * 
     * @author gujianfeng
     * @date 2013-5-27 上午 16:00:00
     */
    @RequestMapping("ajaxpurcherType")
    @ResponseBody
    public String purcherType(PurchaserOrderVo vo, PageQuery pageQuery) {
        PageFinder<PurchaserOrderVo> pageFinder = this.pot.getPurchaser(vo, pageQuery);
        ModelMap map = new ModelMap();
        map.put("Rows", pageFinder.getData());
        map.put("Total", pageFinder.getRowCount());
        return JsonHelper2.toJson(map);
    }

    /**
     * 采购单类型保存
     */
    @RequestMapping("purcherTypeSave")
    @ResponseBody
    public String purcherTypeSave(PurchaserOrderVo vo) {
        try {
            if (this.iPurchaserOrder.get("purchaseType", vo.getPurchaseType()) != null) {
                return SZSCMConstant.FAIL;
            }
            PurchaserOrder po = new PurchaserOrder();
            po.setCode(POPropertyUtils.genPurchaseOrderNo());
            po.setDeleteFlag(1);
            po.setPeriodValidity(Integer.valueOf(vo.getPeriodValidity()));
            po.setPurchaseType(Integer.valueOf(vo.getPurchaseType()));
            this.iPurchaserOrder.save(po);
        } catch (Exception e) {
            e.printStackTrace();
            return SZSCMConstant.FAIL;
        }
        return SZSCMConstant.SUCCESS;
    }

    /**
     * 跳转到修改单据有效设置
     * 
     * @param id
     * @param map
     * @return
     */
    @RequestMapping("toUpdateReceipts")
    public String toUpdateReceipts(String id, ModelMap map) {

        PurchaserOrder order = this.purchaseOrderTypeService.getPurchaserOrderById(id);
        map.put("purchaserOrder", order);
        return "/base/receipts/update_receipts";
    }

    /**
     * 修改单据有效设置
     * 
     * @param purchaserOrder
     * @return
     * @throws Exception
     */
    @RequestMapping("updateReceipts")
    @ResponseBody
    public String updateReceipts(PurchaserOrder purchaserOrder) throws Exception {
        if (null != purchaserOrder) {
            this.purchaseOrderTypeService.getByIdUpdateReceipts(purchaserOrder);
            return SZSCMConstant.SUCCESS;
        }
        return SZSCMConstant.FAIL;
    }

}
